package timetest;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

/**
 * @author shentianyu
 * @date 2021-06-28 20:59
 */
public class TestLocalDateTime {
    public static void main(String[] args) throws InterruptedException {
        //LocalDate  LocalTime  LocalDateTime
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);

        LocalDateTime time = LocalDateTime.of(2021, 6, 28, 22, 10, 15);
        System.out.println(time);

        LocalDateTime localDateTime = ldt.plusYears(1);
        System.out.println(localDateTime);

        LocalDateTime dateTime = ldt.minusYears(1);
        System.out.println(dateTime);

        //Instant时间戳
        //以Unix元年 1970年1月1日0时0分0秒到某个时间的毫秒值
        Instant instant = Instant.now();//格林威治时间
        System.out.println(instant);

        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        System.out.println(instant.toEpochMilli());
        System.out.println("------------------");

        //Duration：计算两个时间之间的间隔
        //Period：计算两个日期之间的间隔
        Instant instant1 = Instant.now();
        //Thread.sleep(1000);
        Instant instant2 = Instant.now();

        System.out.println(Duration.between(instant1, instant2).toMillis());

        System.out.println("-----------------");

        LocalTime time1 = LocalTime.now();
        //Thread.sleep(1000);
        LocalTime time2 = LocalTime.now();
        System.out.println(Duration.between(time1, time2).toMillis());

        System.out.println("-----------------");

        LocalDate date2 = LocalDate.now();
        LocalDate date1 = LocalDate.of(2014,7,1);
        System.out.println(Period.between(date1, date2).toTotalMonths());


        //时间校正器 TemporalAdjuster
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime1 = now.withDayOfMonth(10);
        System.out.println(now);
        System.out.println(localDateTime1);

        LocalDateTime localDateTime2 = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(localDateTime2);

        //DateTimeFormatter   格式化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE;
        LocalDateTime time3 = LocalDateTime.now();
        String format = time3.format(dateTimeFormatter);
        System.out.println(time3.format(dateTimeFormatter));

        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        System.out.println(time3.format(dateTimeFormatter1));
        System.out.println(dateTimeFormatter1.format(time3));


    }
}
